package com.example.echoewish.ui.my_activity.chat_activity;

import androidx.annotation.RequiresApi;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.app.Activity;
import android.app.Application;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.widget.Toolbar;

import com.example.echoewish.R;
import com.example.echoewish.econstant.Constant;
import com.example.echoewish.econstant.UserInfo;
import com.example.echoewish.pojo.Chat;
import com.example.echoewish.pojo.ChatEntity;
import com.example.echoewish.pojo.MyConcern;
import com.example.echoewish.ui.EchoWishCompatActivity;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

public class ChatActivity extends EchoWishCompatActivity {
    private final static String OTHER_ID="otherId";
    private final static String FRIEND_NAME="name";
    private final static String FRIEND_IMAGE="image";

    private ListView chatListView;
    private EditText replyEditText;
    private Button replyButton;
    private ChatAdapter chatAdapter;
    private Toolbar toolbar;


    private ChatViewModel chatViewModel;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        MyConcern myConcern=new MyConcern();
        myConcern.friId=getIntent().getIntExtra(OTHER_ID,-1);
        myConcern.headImage=getIntent().getStringExtra(FRIEND_IMAGE);
        myConcern.name=getIntent().getStringExtra(FRIEND_NAME);
        chatViewModel.otherMutableLiveData.setValue(myConcern);
    }

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_chat);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void initView() {

        chatListView=findViewById(R.id.my_chat_list_view);
        replyEditText=findViewById(R.id.et_content);
        replyButton=findViewById(R.id.btn_send);
        toolbar=findViewById(R.id.chat_view_tool_bar);

        toolbar.inflateMenu(R.menu.chat_view_menu);
        chatAdapter=new ChatAdapter(this,0,new LinkedList<>());
        chatListView.setAdapter(chatAdapter);

        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {
            case R.id.toolbar_delete_chat_message:
                chatViewModel.deleteChatMessage();
                Toast.makeText(ChatActivity.this,"删除聊天记录成功",Toast.LENGTH_SHORT).show();
                break;
            }
            return true;
            }
        });

        replyButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content=replyEditText.getText().toString();
                if(content==null||content.equals(""))
                    Toast.makeText(ChatActivity.this,"发送内容不能为空",Toast.LENGTH_SHORT).show();
                else
                {
                    chatViewModel.replyMutableLiveData.setValue(content);
                    replyEditText.setText("");
                }
            }
        });

    }

//    public boolean onOptionsItemSelected(MenuItem item) {
//        switch (item.getItemId()) {
//            case R.id.toolbar_delete_chat_message:
//                Toast.makeText(this,"删除聊天记录",Toast.LENGTH_SHORT).show();
//                break;
//        }
//        return super.onOptionsItemSelected(item);
//    }

    public static void activityStart(Activity activity, MyConcern myConcern)
    {
        Intent intent=new Intent(activity,ChatActivity.class);
        intent.putExtra(OTHER_ID,myConcern.friId);
        intent.putExtra(FRIEND_NAME,myConcern.name);
        intent.putExtra(FRIEND_IMAGE,myConcern.headImage);
        activity.startActivity(intent);
    }

    @Override
    public void initViewModel() {
        chatViewModel=new ViewModelProvider(this,new ViewModelProvider.AndroidViewModelFactory(new Application())).get(ChatViewModel.class);
    }

    @Override
    public void observeLiveData() {
        chatViewModel.otherMutableLiveData.observe(this, new Observer<MyConcern>() {
            @Override
            public void onChanged(MyConcern myConcern) {
                chatViewModel.initChatMessageRepository();
            }
        });

        chatViewModel.replyMutableLiveData.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                chatViewModel.sendMessage();
                Chat chat=new Chat();
                Date time=new Date();
                SimpleDateFormat simpleDateFormat=new SimpleDateFormat(Constant.FILE_NAME_OR_COMMENT_FORMAT);
                chat.time=simpleDateFormat.format(time);
                chat.headImage= UserInfo.getInstance().headImage;
                chat.content=s;
                chat.name=UserInfo.getInstance().name;
                chat.id=UserInfo.getInstance().UserId;
                chat.otherId=chatViewModel.otherMutableLiveData.getValue().friId;
                chatViewModel.chatsMutableLiveData.getValue().add(chat);
                chatViewModel.storeInDatabase(chat);
                chatViewModel.chatsMutableLiveData.postValue(chatViewModel.chatsMutableLiveData.getValue());
            }
        });

        chatViewModel.otherReplyMutableLiveData.observe(this, new Observer<Chat>() {
            @Override
            public void onChanged(Chat getChat) {
                Chat chat=new Chat();
                chat.headImage= chatViewModel.otherMutableLiveData.getValue().headImage;
                chat.content=getChat.content;
                chat.time=getChat.time;
                chat.name=chatViewModel.otherMutableLiveData.getValue().name;
                chat.id=chatViewModel.otherMutableLiveData.getValue().friId;
                chat.otherId=UserInfo.getInstance().UserId;
                chatViewModel.chatsMutableLiveData.getValue().add(chat);
                chatViewModel.storeInDatabase(chat);
                chatViewModel.chatsMutableLiveData.postValue(chatViewModel.chatsMutableLiveData.getValue());
            }
        });

        chatViewModel.chatEntityMutableLiveData.observe(this, new Observer<List<ChatEntity>>() {
            @Override
            public void onChanged(List<ChatEntity> chatEntities) {
                List<Chat>chatList=new LinkedList<>();
                for(ChatEntity chatEntity:chatEntities)
                {
                    Chat chat=new Chat(chatEntity);
                    if(chat.id==UserInfo.getInstance().UserId)
                    {
                        chat.headImage= UserInfo.getInstance().headImage;
                        chat.name=UserInfo.getInstance().name;
                    }
                    else
                    {
                        chat.headImage= chatViewModel.otherMutableLiveData.getValue().headImage;
                        chat.name=chatViewModel.otherMutableLiveData.getValue().name;
                    }
                    chatList.add(chat);
                }
                chatViewModel.chatsMutableLiveData.getValue().addAll(chatList);
                chatViewModel.chatsMutableLiveData.postValue(chatViewModel.chatsMutableLiveData.getValue());
            }
        });

        chatViewModel.chatsMutableLiveData.observe(this, new Observer<List<Chat>>() {
            @Override
            public void onChanged(List<Chat> chats) {
//                if(chats.size()==0)
//                    return;
                chatAdapter.clear();
                chatAdapter.addAll(chats);
                chatAdapter.notifyDataSetChanged();
            }
        });

    }

    @Override
    protected void onDestroy() {
        chatViewModel.closeConnect();
        super.onDestroy();
    }
}